<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="教师姓名" prop="teacherName">
        <el-input
          v-model="queryParams.teacherName"
          placeholder="请输入教师姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="活动" prop="evalutionActivityId">
        <el-select v-model="queryParams.evalutionActivityId" placeholder="请选择活动" clearable>
          <el-option
            v-for="activity in activityList"
            :key="activity.evalutionActivityId"
            :label="activity.title"
            :value="activity.evalutionActivityId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="院系名称" prop="departmentName" v-show="false">
        <el-input
          v-model="queryParams.departmentName"
          placeholder="请输入院系名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="归属部门">
        <treeselect v-model="deptId" :options="deptOptions" :show-count="true" style="width: 180px"
                    placeholder="请选择归属部门" @select="handleTableNodeClick" clearable @input="handleCloseClick"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['pingjiao:statistic:list']"
        >导出
        </el-button>
      </el-form-item>
    </el-form>

    <el-divider><i class="el-icon-user-solid"></i>教师评价结果统计</el-divider>

    <el-table v-loading="loading" :data="statisticsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="序号" align="center" type="index" prop="id"/>
      <el-table-column label="被评教师" align="center" prop="realName">
        <template slot-scope="scope">
          <router-link :to="'/pingjiao/evaluationProgress/courseclassList/getEvaluationProgressList/'+ scope.row.teacherId" class="link-type">
            <span>{{ scope.row.realName }}</span>
          </router-link>
        </template>
      </el-table-column>
      <el-table-column label="工号" align="center" prop="teacherNo"/>
      <el-table-column label="院系" align="center" prop="deptName"/>
      <el-table-column label="得分" align="center" prop="score"/>
      <el-table-column label="名次" align="center" prop="rankNo"/>
      <el-table-column label="应评教人数" align="center" prop="stuNum"/>
      <el-table-column label="已评教人数" align="center" prop="doneNum"/>
      <el-table-column label="评教完成度" align="center" >
        <template slot-scope="scope">
          <el-progress :percentage="scope.row.votingRate" :format="format"></el-progress>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-document-checked"
            @click="gotoDetail(scope.row)"
            v-hasPermi="['pingjiao:statistics:list']"
          >查看得分详情
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改课程管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-table v-loading="loading" :data="detailList" height="350">
        <el-table-column label="项目" align="center" prop="paperItemTitle"/>
        <el-table-column label="得分" align="center" prop="score"/>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import {
  listTeacherStatistics,
  getStatisticsDetails,
  exportListTeacherStatistics,
  getStatistics,
  delStatistics,
  addStatistics,
  updateStatistics
} from "@/api/pingjiao/statistics";
import {listActivity} from "@/api/pingjiao/activity";
import {deptTreeSelect} from "@/api/system/user";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Teacher",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      //明细列表
      detailList: [],
      // 统计表格数据
      statisticsList: [],
      // 评教活动管理表格数据
      activityList: [],
      // 部门树选项
      deptOptions: [],
      //部门ID
      deptId: undefined,
      // 部门名称
      deptName: undefined,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        teacherName: null,
        departmentName: null,
        evalutionActivityId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  components: {Treeselect},
  watch: {
    // 根据名称筛选部门树
    deptName(val) {
      this.$refs.tree.filter(val);
    }
  },
  created() {
    this.getDeptTree();
    this.getList();
    this.getActivityList();
  },
  methods: {
    /** 查询统计列表 */
    getList() {
      this.loading = true;
      listTeacherStatistics(this.queryParams).then(response => {
        console.log("teacher statisticsList", response);
        this.statisticsList = response.rows;
		console.log(this.statisticsList)
        this.total = response.total;
        this.loading = false;
      });
      this.loading = false;
    },
    /** 查询评教活动管理列表 */
    getActivityList() {
      this.loading = true;
      listActivity(this.queryParams).then(response => {
        this.activityList = response.rows;
        console.log(this.statisticsList)
        this.loading = false;
      });
    },
    /** 查询部门下拉树结构 */
    getDeptTree() {
      deptTreeSelect().then(response => {
        console.log(response.data);
        this.deptOptions = response.data;
      });
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.deptId = data.id;
    },
    // 插入或修改提升框节点单击事件
    handleTableNodeClick(data) {
      console.log(data);
      this.queryParams.departmentName = data.label;
    },
    handleCloseClick(data) {
      if (data == null){
        this.queryParams.departmentName = null;
      }
    },

    gotoDetail(row) {//查看详情
      this.detailList = null;
      let evalutionActivityId = this.queryParams.evalutionActivityId;
      let teacherId = row.teacherId

      console.log("=============================")
      console.log(evalutionActivityId)
      console.log(teacherId)

      getStatisticsDetails(evalutionActivityId, teacherId).then(response => {
        console.log(response);
        this.detailList = response.data;
        this.detailList = this.detailList.map((item) => {
          item.score = item.score.toFixed(1);
          return item;
        })
        this.open = true;
        this.title = "评教明细";
        console.log(this.detailList);
      });
    },
    format(percentage) {
      return percentage === 100 ? '满' : `${percentage}%`;
    },
    // 表单重置
    reset() {
      this.deptId = undefined;
      this.form = {
        evaluationId: null,
        evaluationActivityId: null,
        evaluationPaperId: null,
        teacherId: null,
        courseclassId: null,
        studentId: null,
        status: "0",
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        generateTime: null,
        sumScore: null,
        state: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.deptId = undefined;
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleExport(){
      this.download('pingjiao/statistic/export', {
        ...this.queryParams
      }, `统计分析_${new Date().getTime()}.xlsx`)
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.evaluationId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },

  }
};
</script>
